﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using ICSharpCode.AvalonEdit.Document;
using IPRMIS_U_Contract;
using Layer;
using PremisPublisher.Common;
using PremisPublisher.Messages;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;

namespace PremisPublisher.ViewModel
{
    /*
     
 sql编辑主Model
 * 
 * 作者：张鹏
 * 日期：2016.5
     
 */

    public class SQLContentEditor : ViewModelBase
    {
        private string _sqlName;

        public string SQLName
        {
            get { return _sqlName; }
            set { _sqlName = value; RaisePropertyChanged("SQLName"); }
        }



        private TextDocument _sqlcontent;

        public TextDocument SQLContent
        {
            get { return _sqlcontent; }
            set { _sqlcontent = value; RaisePropertyChanged("SQLContent"); }
        }

        public ICommand SaveAndUploadCommand { get; set; }

        public SQLContentEditor()
        {
            SQLContent = new TextDocument();
            SaveAndUploadCommand = new RelayCommand(SaveAndUpload);
        }

        private void SaveAndUpload()
        {
            //加密，上传

            var sql = SQLContent.Text;

            UpgradeMessage msg = new UpgradeMessage();
            msg.Name = SQLName;
            msg.Message = RSACrypto.Encrypt(sql, crypt_sql_files_reader.GetInstance().GetRSAPublicKey());

            ServiceCaller.TcpServiceDispatcher<IBusinessUpgrade>("0099", (svc) =>
            {
                msg = svc.UploadSQLContent(msg);
            });

            Messenger.Default.Send(new UploadResultMessage() { Code = msg.Code, Message = msg.Message });
        }
    }
}
